import 'package:enjoy_plus_community/apis/get_location.dart';
import 'package:enjoy_plus_community/apis/get_nearby_communities.dart';
import 'package:enjoy_plus_community/apis/reverse_get_coding.dart';
import 'package:enjoy_plus_community/routes/index.dart';
import 'package:flutter/material.dart';

class LocationList extends StatefulWidget {
  const LocationList({super.key});

  @override
  State<LocationList> createState() => _LocationListState();
}

class _LocationListState extends State<LocationList> {
  String location = '';
  List nearbyCommunities = [];

  @override
  void initState() {
    super.initState();
    // getCurrentLocation();
    getLocation();
    getNearBy();
  }

  getLocation() async {
    final position = await reverseGeocoding(40.065956, 116.350077);
    setState(() {
      location = position;
    });
  }

  getNearBy() async {
    final nearBy = await queryNearbyCommunities(40.065956, 116.350077);
    setState(() {
      nearbyCommunities = nearBy;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('选择社区')),
      body: ListView(
        padding: const EdgeInsets.all(10),
        children: [
          const Row(
            children: [Text('当前地址', style: TextStyle(color: Colors.grey))],
          ),
          const SizedBox(height: 10),
          Container(
            color: Colors.white,
            padding: const EdgeInsets.all(10),
            child: Row(
              children: [
                Expanded(child: Text(location)),
                GestureDetector(
                  onTap: () {
                    getLocation();
                  },
                  child: Row(
                    children: [
                      Icon(
                        Icons.location_searching_outlined,
                        color: Colors.blue,
                      ),
                      SizedBox(width: 2),
                      Text('重新定位', style: TextStyle(color: Colors.blue)),
                    ],
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 10),
          const Row(
            children: [Text('附近社区', style: TextStyle(color: Colors.grey))],
          ),
          const SizedBox(height: 10),
          ListView(
            // 设置为 true 时，ListView 会根据其 子项的总高度/宽度 自动调整自身尺寸，不再占用多余空间
            // 当 ListView 作为另一个滚动组件（如 Column 或 SingleChildScrollView）的子项时，避免滚动冲突
            shrinkWrap: true,
            // 完全禁用 ListView 的滚动行为，列表内容不可滑动（通常与 shrinkWrap: true 配合使用）
            physics: const NeverScrollableScrollPhysics(),
            children: nearbyCommunities.map((item) {
              return GestureDetector(
                onTap: () {
                  Navigator.pushNamed(context, AppRouter.buildingList, arguments: {'point': item['name']});
                },
                child: Container(
                  color: Colors.white,
                  padding: const EdgeInsets.all(10),
                  child: Row(
                    children: [
                      Expanded(child: Text(item['name'])),
                      Row(
                        children: [
                          Icon(
                            Icons.arrow_forward_ios,
                            size: 16,
                            color: Colors.black,
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              );
            }).toList(),
          ),
        ],
      ),
    );
  }
}
